﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RESTServices.Dominio;
using System.Data.SqlClient;

namespace RESTServices.Persistencia
{
    public class ReservaDAO
    {
        public Reserva Crear(Reserva reservaCrear)
        {
            Reserva reservaCreada = null;
            string sql = "INSERT INTO t_reserva VALUES (@id, @correo, @idcar, @cantn, @canta)";
            using (SqlConnection con = new SqlConnection(ConexionUtil.Cadena))
            {
                con.Open();
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    com.Parameters.Add(new SqlParameter("@id", reservaCrear.Idreserva));
                    com.Parameters.Add(new SqlParameter("@correo", reservaCrear.Correo));
                    com.Parameters.Add(new SqlParameter("@idcar", reservaCrear.Idcartelera));
                    com.Parameters.Add(new SqlParameter("@cantn", reservaCrear.Cantn));
                    com.Parameters.Add(new SqlParameter("@canta", reservaCrear.Canta));
                    com.ExecuteNonQuery();

                }
            }

            reservaCreada = Obtener(reservaCrear.Idreserva);
            return reservaCreada;
        }
        public Reserva Obtener(int idreserva)
        {
            Reserva reservaEncontrada = null;
            string sql = "SELECT * FROM t_reserva WHERE idreserva=@id";
            using (SqlConnection con = new SqlConnection(ConexionUtil.Cadena))
            {
                con.Open();
                using (SqlCommand com = new SqlCommand(sql, con))
                {

                    com.Parameters.Add(new SqlParameter("@id", idreserva));
                    using (SqlDataReader resultado = com.ExecuteReader())
                    {

                        if (resultado.Read())
                        {

                            reservaEncontrada = new Reserva()
                            {
                                Idreserva = (int)resultado["idreserva"],
                                Correo = (string)resultado["correo"],
                                Idcartelera = (int)resultado["idcartelera"],
                                Cantn = (int)resultado["cantn"],
                                Canta = (int)resultado["canta"],
                            };
                        }
                    }
                }
            }

            return reservaEncontrada;
        }

        public Reserva Modificar(Reserva reservaModificar)
        {

            Reserva reservaModificada = null;
            string sql = "UPDATE t_reserva SET correo=@correo, idcartelera=@idcar,cantn=@cantn,canta=@canta WHERE idcartelera=@id";
            using (SqlConnection con = new SqlConnection(ConexionUtil.Cadena))
            {
                con.Open();
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    com.Parameters.Add(new SqlParameter("@id", reservaModificar.Idreserva));
                    com.Parameters.Add(new SqlParameter("@correo", reservaModificar.Correo));
                    com.Parameters.Add(new SqlParameter("@idcar", reservaModificar.Idcartelera));
                    com.Parameters.Add(new SqlParameter("@Cantn", reservaModificar.Cantn));
                    com.Parameters.Add(new SqlParameter("@Canta", reservaModificar.Canta));

                    com.ExecuteNonQuery();

                }
            }

            reservaModificada = Obtener(reservaModificar.Idreserva);
            return reservaModificada;
        }

        public void Eliminar(Reserva reservaEliminar)
        {

            Reserva reservaEliminada = null;
            string sql = "DELETE FROM t_reserva WHERE Idreserva=@cod";
            using (SqlConnection con = new SqlConnection(ConexionUtil.Cadena))
            {
                con.Open();
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    com.Parameters.Add(new SqlParameter("@cod", reservaEliminar.Idreserva));
                    com.ExecuteNonQuery();

                }
            }
            reservaEliminada = Obtener(reservaEliminar.Idreserva);
        }

        public List<Reserva> ListarTodos()
        {
            Reserva al = null;
            List<Reserva> traeReserva = new List<Reserva>();
            string sql = "SELECT * FROM  T_reserva";
            using (SqlConnection con = new SqlConnection(ConexionUtil.Cadena))
            {
                con.Open();
                using (SqlCommand com = new SqlCommand(sql, con))
                {

                    using (SqlDataReader resultado = com.ExecuteReader())
                        if (resultado.HasRows)
                        {
                            while (resultado.Read())
                            {
                                al = new Reserva()
                                {
                                    Idreserva = (int)resultado["Idreserva"],
                                    Correo = (string)resultado["correo"],
                                    Idcartelera = (int)resultado["Idcartelera"],
                                    Cantn = (int)resultado["cantn"],
                                    Canta = (int)resultado["canta"],
                                };
                                traeReserva.Add(al);
                            }
                        }

                }
            }
            return traeReserva;
        }
    }
}